#include "MainMenuLayer.h"
#include "GameScene.h"
#include "PlayHavenHelper.h"

MainMenuLayer::MainMenuLayer()
{
}

MainMenuLayer::~MainMenuLayer()
{
}

bool MainMenuLayer::init()
{
  do
  {
    CC_BREAK_IF(!CCLayer::init());

    CCLabelTTF *pResumeLabel = CCLabelTTF::create("Play", "Arial", 40);
    CC_BREAK_IF(!pResumeLabel);
    CCMenuItemLabel * pResumeItemLabel = CCMenuItemLabel::create(pResumeLabel, this, menu_selector(MainMenuLayer::onPlay));
    CC_BREAK_IF(!pResumeItemLabel);
    pResumeItemLabel->setPositionY(100);

    CCLabelTTF *pMoreGameLabel = CCLabelTTF::create("More Game", "Arial", 40);
    CC_BREAK_IF(!pMoreGameLabel);
    CCMenuItemLabel * pMoreGameItemLabel = CCMenuItemLabel::create(pMoreGameLabel, this, menu_selector(MainMenuLayer::onMoreGame));
    CC_BREAK_IF(!pMoreGameItemLabel);
    pMoreGameItemLabel->setPositionY(0);

    CCLabelTTF *pExitLabel = CCLabelTTF::create("Exit", "Arial", 40);
    CC_BREAK_IF(!pExitLabel);
    CCMenuItemLabel * pExitItemLabel = CCMenuItemLabel::create(pExitLabel, this, menu_selector(MainMenuLayer::onExit));
    CC_BREAK_IF(!pExitItemLabel);
    pExitItemLabel->setPositionY(-100);

    CCMenu * pMenu = CCMenu::create(pResumeItemLabel, pMoreGameItemLabel, pExitItemLabel,  NULL);
    CC_BREAK_IF(!pMenu);
    addChild(pMenu);

    return  true;
  }
  while(0);
  return false;
}

MainMenuLayer * MainMenuLayer::create()
{
  MainMenuLayer * pLayer = new MainMenuLayer();
  if (pLayer && pLayer->init())
  {
    pLayer->autorelease();
    return pLayer;
  }
  else
  {
    CC_SAFE_DELETE(pLayer);
    return NULL;
  }
}

void MainMenuLayer::onPlay(CCObject * pObj)
{
  CCDirector::sharedDirector()->replaceScene(GameScene::create());
}

void MainMenuLayer::onExit(CCObject * pObj)
{
  CCDirector::sharedDirector()->end();
}

void MainMenuLayer::onMoreGame(CCObject * pObj)
{
  PlayHavenHelper::getInstance()->startList("moregame");
}
